package com.budwk.app.miniapp.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.miniapp.models.SysDictData;
import com.budwk.app.miniapp.services.SysdictdataService;
import com.budwk.starter.websocket.publish.RedisMessagePubSub;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/miniapp/dictData")
@ApiDefinition(tag = "微信小程序-数据字典管理")
@Slf4j
public class SysdictdataController {
    @Inject
    private SysdictdataService sysDictDataService;



    /**
     * 分页查询数据字典
     * @param bean
     * @param pageNo
     * @param pageSize
     * @param pageOrderName
     * @param pageOrderBy
     * @return
     */
    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询数据字典")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = SysDictData.class
    )
    @ApiResponses(
            implementation = SysDictData.class
    )
    @SaCheckLogin
    public Result<?> list(@Param("..") SysDictData bean,
                          @Param("pageNo") int pageNo,
                          @Param("pageSize") int pageSize,
                          @Param("pageOrderName") String pageOrderName,
                          @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }

        if (bean.getDictSort() != null && Strings.isNotBlank(bean.getDictSort().toString())) {
            cnd.and("dict_sort", "=", bean.getDictSort());
        }

        if (bean.getDictLabel() != null && Strings.isNotBlank(bean.getDictLabel().toString())) {
            cnd.and("dict_label", "=", bean.getDictLabel());
        }

        if (bean.getDictValue() != null && Strings.isNotBlank(bean.getDictValue().toString())) {
            cnd.and("dict_value", "=", bean.getDictValue());
        }

        if (bean.getDictType() != null && Strings.isNotBlank(bean.getDictType().toString())) {
            cnd.and("dict_type", "=", bean.getDictType());
        }

        if (bean.getCssClass() != null && Strings.isNotBlank(bean.getCssClass().toString())) {
            cnd.and("css_class", "=", bean.getCssClass());
        }

        if (bean.getListClass() != null && Strings.isNotBlank(bean.getListClass().toString())) {
            cnd.and("list_class", "=", bean.getListClass());
        }

        if (bean.getIsDefault() != null && Strings.isNotBlank(bean.getIsDefault().toString())) {
            cnd.and("is_default", "=", bean.getIsDefault());
        }

        if (bean.getStatus() != null && Strings.isNotBlank(bean.getStatus().toString())) {
            cnd.and("status", "=", bean.getStatus());
        }

        if (bean.getRemark() != null && Strings.isNotBlank(bean.getRemark().toString())) {
            cnd.and("remark", "=", bean.getRemark());
        }

        if (bean.getParentCode() != null && Strings.isNotBlank(bean.getParentCode().toString())) {
            cnd.and("parent_code", "=", bean.getParentCode());
        }
        RedisMessagePubSub  d=new RedisMessagePubSub();
        return Result.data(sysDictDataService.listPage(pageNo, pageSize, cnd));
    }

    /**
     * 新增数据字典
     * @param sysDictData
     * @param req
     * @return
     */
    @At("/add")
    @Ok("json")
    @POST
    @ApiOperation(name = "新增数据字典")
    @ApiFormParams(
            implementation = SysDictData.class
    )
    @ApiResponses
    @SLog(tag = "新增SysDictData", value = "${args[0].dictCode}")
    @SaCheckLogin
    @Aop(TransAop.READ_COMMITTED)
    public Result<?> create(@Param("..") SysDictData sysDictData, HttpServletRequest req) {
        sysDictData.setCreateBy(SecurityUtil.getUserId());
        sysDictDataService.insert(sysDictData);
        return Result.success();
    }

    /**
     * 修改数据字典
     * @param sysDictData
     * @param req
     * @return
     */
    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改数据字典")
    @ApiFormParams(
            implementation = SysDictData.class
    )
    @ApiResponses
    @SLog(tag = "修改SysDictData", value = "${args[0].dictCode}")
    @SaCheckLogin
    @Aop(TransAop.READ_COMMITTED)
    public Result<?> update(@Param("..") SysDictData sysDictData, HttpServletRequest req) {
        sysDictData.setUpdateBy(SecurityUtil.getUserId());
        sysDictDataService.updateIgnoreNull(sysDictData);
        return Result.success();
    }

    /**
     * 获取数据字典明细
     * @param id
     * @param req
     * @return
     */
    @At("/detail/{id}")
    @Ok("json")
    @GET
    @ApiOperation(name = "获取数据字典明细")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SaCheckLogin
    public Result<?> getData(@Param("id") String id, HttpServletRequest req) {
        SysDictData sysDictData = sysDictDataService.fetch(Cnd.where("dict_code", "=", id));
        if (sysDictData == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(sysDictData);
    }

    /**
     * 删除数据字典
     * @param id
     * @param req
     * @return
     */
    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除数据字典")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除SysDictData", value = "${args[0]}")
    @SaCheckLogin
    @Aop(TransAop.READ_COMMITTED)
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        SysDictData sysDictData = sysDictDataService.fetch(id);
        if (sysDictData == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        sysDictDataService.delete(id);
        req.setAttribute("_slog_msg", sysDictData.getDictCode());
        return Result.success();
    }
    @At("/listByDictType")
    @Ok("json")
    @POST
    public Result<?> listByDictType(@Param("dictType") String dictType){
        return Result.data(sysDictDataService.selectDictDataByType(dictType));
    }

}
